/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.user.provider.hibernate3.ahxu.impl;

import com.opensymphony.user.provider.hibernate3.ahxu.entity.BaseHibernateEntity;
import com.opensymphony.user.provider.hibernate3.ahxu.entity.HibernateGroup;
import com.opensymphony.user.provider.hibernate3.ahxu.entity.HibernateUser;

import java.util.*;


/**
 * @author <a href="mailto:matthew@linjafoo.com">Matthew E. Porter</a>
 */
public class HibernateGroupImpl extends BaseHibernateEntity implements HibernateGroup {
    //~ Instance fields ////////////////////////////////////////////////////////

    private Set users = null;

    //~ Methods ////////////////////////////////////////////////////////////////

    public List getUserList() {
        ArrayList userList = new ArrayList();

        if (this.users != null) {
            userList.addAll(this.users);
        }

        return userList;
    }

    public List getUserNameList() {
        ArrayList userNameList = new ArrayList();

        if ((this.users != null) && (this.users.size() > 0)) {
            Iterator userIter = this.users.iterator();

            while (userIter.hasNext()) {
                HibernateUser user = (HibernateUser) userIter.next();
                userNameList.add(user.getName());
            }
        }

        return userNameList;
    }

    public void setUsers(Set users) {
        this.users = users;
    }

    public Set getUsers() {
        return users;
    }

    public void addUser(HibernateUser user) {
        if (users == null) {
            users = new HashSet(1);
        }

        users.add(user);
    }

    public boolean removeUser(HibernateUser user) {
        if (user != null) {
            return users.remove(user);
        } else {
            return false;
        }
    }

    public boolean removeUser(String username) {
        if (users != null) {
            Iterator userIter = users.iterator();

            while (userIter.hasNext()) {
                HibernateUser user = (HibernateUser) userIter.next();

                if (user.getName().equals(username)) {
                    return removeUser(user);
                }
            }
        }

        return false;
    }
}
